import React, { Component } from 'react'
import { nanoid } from 'nanoid'
import './index.css'

export default class Header extends Component {

  handleKeyUp = (event) => {
    const { keyCode, target } = event
    if (event.keyCode !== 13) {
      return
    }
    console.log(target.value, keyCode)
    const todoObj = { id: nanoid(), name: target.value, done: false }
    console.log('添加todo', todoObj)
    this.props.addTodo(todoObj)
    target.value = ''
  }

  render() {
    return (
      <div className="todo-header">
        <input type="text" placeholder="请输入你的任务名称，按回车键确认" onKeyUp={this.handleKeyUp} />
      </div>
    )
  }
}
